package org.shilong.UI.party;

import org.shilong.UI.R;
import org.shilong.UI.login.LoginFrame;
import org.shilong.Utility.SharedPreferenceUtility;
import org.taptwo.android.widget.TitleFlowIndicator;
import org.taptwo.android.widget.TitleProvider;
import org.taptwo.android.widget.ViewFlow;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 新的聚会中心布局 重新来 哥们你是好样的！！！
 * 
 * @author cesc
 * 
 */
public class BrandPartyCenterFrame extends ActivityGroup {

	private ViewFlow viewFlow;

	private TitleFlowIndicator indicator;

	private LocalActivityManager manager;

	public final static String ACTION_REFRESH_MY_PAREIS_DATAS = "MY";

	public final static String ACTION_REFRESH_NEW_PAREIS_DATAS = "NOW";

	public final static String ACTION_REFRESH_OLD_PAREIS_DATAS = "OLD";

	/* 用户个人信息 */
	private String[] userInfo;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.new_layout_party_center_content);

		userInfo = SharedPreferenceUtility
				.getPersonInfoFromSharedPreference(this);

		initViews();

		registerEvents();

	}

	private void initViews() {
		// TODO Auto-generated method stub

		viewFlow = (ViewFlow) this.findViewById(R.id.party_center_viewflow);

		indicator = (TitleFlowIndicator) this
				.findViewById(R.id.party_center_viewflowindic);

		manager = getLocalActivityManager();

	}

	private void registerEvents() {
		// TODO Auto-generated method stub

		Class<?>[] arr = new Class<?>[] { BrandShowNewPartyFrame.class,
				BrandShowHistoryPartyFrame.class, BrandNewMyPariesFrame.class };

		viewFlow.setAdapter(new LayoutAdapter(arr));

		indicator.setTitleProvider(new PartyCenterTitle());
		viewFlow.setFlowIndicator(indicator);

		viewFlow.setOnViewSwitchListener(new ViewFlow.ViewSwitchListener() {

			@Override
			public void onSwitched(View view, int position) {
				// TODO Auto-generated method stub

				Intent it = new Intent();
				switch (position) {
				case 0: // 新聚会

					it.setAction(ACTION_REFRESH_NEW_PAREIS_DATAS);


					System.out.println("跳转新聚会");
					break;
				case 1: // 历史聚会
					it.setAction(ACTION_REFRESH_OLD_PAREIS_DATAS);
					System.out.println("跳转历史聚会");

					break;
				case 2: // 我的聚会

					it.setAction(ACTION_REFRESH_MY_PAREIS_DATAS);
					System.out.println("跳转我的聚会");
					break;

				default:
					break;
				}

				BrandPartyCenterFrame.this.sendBroadcast(it);
				//BrandPartyCenterFrame.this.sendBroadcast(it);
			}
		});
	}

	/**
	 * 布局迭代器
	 */

	private class LayoutAdapter extends BaseAdapter {

		private Class<?>[] clsArr;

		public LayoutAdapter(Class<?>[] arr) {

			this.clsArr = arr;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return clsArr.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return clsArr[arg0];
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {

			Intent mIntent = new Intent();

			mIntent.setClass(BrandPartyCenterFrame.this, clsArr[arg0]);

			View insideView = manager.startActivity("id" + Math.random(),
					mIntent).getDecorView();

			return insideView;
		}
	}

	/**
	 * 聚会的标题选择器
	 * 
	 * @author cesc
	 * 
	 */
	private class PartyCenterTitle implements TitleProvider {

		private String[] arr;

		public PartyCenterTitle() {

			arr = new String[] { "新聚会", "历史聚会", "我的聚会" };
		}

		@Override
		public String getTitle(int position) {
			// TODO Auto-generated method stub
			return arr[position];
		}

	}
}
